﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mogre;

namespace ProjetSMA.View.Mogre
{
    public class CameraTracker 
    {

        private Camera m_camera;
        private SceneManager m_scene_mngr;
        private Root m_root;
        private Entity3D m_tracker;
        private string m_name = "";


        public CameraTracker(SceneManager par_sc, Root par_root, Entity3D par_tr, string par_name)
        {
            m_root = par_root;
            m_scene_mngr = par_sc;
            m_tracker = par_tr;
            m_name = par_name;
        }

        public void Initialisation()
        {
            m_root.FrameStarted += new FrameListener.FrameStartedHandler(FollowEntity_FrameStarted);

            m_camera = m_scene_mngr.CreateCamera(m_name);
           // m_camera.Position = new Vector3(0, 0, 0);
           // m_camera.LookAt(new Vector3(0, 0, 0));

        }

        public bool FollowEntity_FrameStarted(FrameEvent evt)
        {
            try
            {
                if (m_tracker != null && m_tracker.Node.Position != null)
                {
                    m_camera.Position = new Vector3(m_tracker.Node.Position.x,
                                                    m_tracker.Node.Position.y + 185,
                                                    m_tracker.Node.Position.z);

                    m_camera.LookAt(new Vector3(m_tracker.Node.Position.x - 18,
                                                0,
                                                m_tracker.Node.Position.z));

                    // m_camera.MoveRelative(new Vector3(0f, 0f, 0f));
                }
            }
            catch (Exception e)
            {

            }

            return true;
        }

        public Camera Camera
        {
            get { return m_camera; }
        }


        public void Destroy()
        {
            m_root.FrameStarted -= new FrameListener.FrameStartedHandler(FollowEntity_FrameStarted);
        }
    }
}
